//
//  MM_DB.m
//  MallManage_IOS
//
//  Created by 张浩 on 2023/9/19.
//

#import "MM_DB.h"

static MM_DB * manager = nil;
static dispatch_once_t onceToken;

@interface MM_DB ()

@property (nonatomic,strong) FMDatabase *dataBase;
/* 网络表结构**/
@property (nonatomic,strong) MM_NetWorkTableTool * netWorkTableTool;

@end

@implementation MM_DB

- (instancetype)init {
    
    if (self = [super init]) {
        
        [self createDB];
        [self createTable];
    }
    return self;
}

+ (MM_DB*)shareManager {
    
    if ([MM_DB canNextStep]) {
        
        dispatch_once(&onceToken, ^{
            
            manager = [[MM_DB alloc]init];
        });
    } else {
        [self attemptDealloc];
    }
    return manager;
}

/* 销毁单例 **/
+ (void)attemptDealloc {
    
    onceToken = 0;
    manager = nil;
}

#pragma mark - private

- (void)createDB {
    
//    /* db name**/ 用户id创建数据库
    NSString * dbName = [[NSString stringWithFormat:@"MM_%@",@"用户id 以后用到"] stringByAppendingString:@".db"];
    /* db path**/
    NSString * dbPath = [[MM_FileTool pathOfResourceType:MM_ResourceTypeOther] stringByAppendingPathComponent:dbName];
    self.dataBase = [FMDatabase databaseWithPath:dbPath];
}

- (void)createTable {
    
    /* 创建网络的表**/
    self.netWorkTableTool  = [[MM_NetWorkTableTool alloc] initWithDB:self.dataBase];
}

/* 数据库问题 **/
+ (BOOL)canNextStep {

    return NO;
    //这里判断当前用户的id是否大于0  是否登录
//    return [JH_UserManager sharedUserManager].infoModel.customer_id.length >0;
}
@end
